package com.shengyin.ec.controller.alliance;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.portlet.ModelAndView;

import com.shengyin.ec.exception.ControllerException;
import com.shengyin.ec.model.entity.Company;
import com.shengyin.ec.model.entity.News;
import com.shengyin.ec.model.entity.Personal;
import com.shengyin.ec.service.account.PersonalService;
import com.shengyin.ec.service.alliance.AllianceService;
import com.shengyin.ec.util.UploadFileUtil;

//add by lcl 2012-09-09
import com.shengyin.ec.controller.alliance.AllianceSupplyParam;
import com.shengyin.ec.model.entity.AllianceSDInfo;
import com.shengyin.ec.service.alliance.AllianceSDInfoService;
//add by lcl 2012-09-13
import com.shengyin.ec.model.entity.ProjectDemandInfo;
import com.shengyin.ec.service.alliance.ProjectDemandInfoService;
//add by lcl 2012-09-14
import com.shengyin.ec.model.entity.AllianceDetail;
import com.shengyin.ec.model.entity.AllianceViewerInfo;
import com.shengyin.ec.service.alliance.AllianceDetailService;
import com.shengyin.ec.service.alliance.AllianceViewInfoService;
import com.shengyin.ec.controller.alliance.AllianceProjectForm;
import com.shengyin.ec.model.entity.ProjectDemandInfo;
import com.shengyin.ec.controller.alliance.AllianceDetailForm;

@Controller
public class AllianceController {

	@Autowired
	AllianceService allianceService;

	@Autowired
	Md5PasswordEncoder passwordEncoder;
  
	@Autowired
	PersonalService personalService;
	
	@Autowired
	AllianceSDInfoService allianceSDInfoService;
	
	@Autowired
	ProjectDemandInfoService projectDemandInfoService;
	
	@Autowired
	AllianceDetailService allianceDetailService;
	
	@Autowired
	AllianceViewInfoService allianceViewInfoService;
	
	 /* 
	@RequestMapping(value = "/allianceValidate.do")
	public String allianceValidate(@RequestParam("username") String username){
		Personal personal = personalService.getPersonalByUsername(username);
		if(personal == "")
			return 
	}
	*/
	@RequestMapping(value = "/toAllianceList.do")
	public String toAllianceList(Model model,HttpServletRequest request) {
		
       int offset = 0;
       try{
    	   offset = Integer.valueOf(request.getParameter("pager.offset"));
    	   

		} catch (Exception ex) {
			System.out.println("初始化页数.......");
		}
       List<Company> companyList = null;
		
			companyList = allianceService.getCompanyList(offset,10);    	  
		
			
			
	    Long maxSize = allianceService.getTotal();
	    System.out.println("================maxSize============================="+maxSize);
		model.addAttribute("companyList", companyList);
		model.addAttribute("maxSize",maxSize);
		return "/admin/alliance/allianceList";
	}

	@RequestMapping(value = "/toAllianceAdd.do")
	public String toAllianceAdd(Model model, HttpServletRequest request)
			throws IOException {

		return "redirect:/view/allianceJoin.jsp";
	}

	@RequestMapping(value = "/toAuditAlliance.do")
	public String toAuditAlliance(Model model, @RequestParam("id")int id) {
		
		Company company = allianceService.getCompanyById(id);
		model.addAttribute("company", company);
		
		return "/admin/alliance/allianceAudit";
	}
	
	
	@RequestMapping(value = "/toViewAlliance.do")
	public String toAllianceView(Model model, @RequestParam("id")int id) {
		
		Company company = allianceService.getCompanyById(id);
		model.addAttribute("company", company);
		
		return "/admin/alliance/allianceView";
	}

	@RequestMapping("/allianceReject.do")
	public String rejectAlliance(@RequestParam("id") long id) {

		return "redirect:/view/alliance/alliance.jsp";
	}

	@RequestMapping("/allianceAdd.do")
	public String createAlliance(AllianceForm allianceForm,HttpServletRequest request,@RequestParam("filename1")MultipartFile filename1)
			throws IllegalAccessException, InvocationTargetException, IOException {

		Company alliance = new Company();

	    allianceForm.setPwd(passwordEncoder.encodePassword("shengyin", allianceForm.getPwd()));
		BeanUtils.copyProperties(alliance, allianceForm);
		alliance.setStatus(0);
		Date createDate = new Date(System.currentTimeMillis());
		alliance.setCreateDate(createDate);
		String filePath =UploadFileUtil.Upload(request, filename1,"alliance");
		alliance.setCertificatesUpload(filePath);
		allianceService.createCompany(alliance);
		return "redirect:/view/alliance/allianceAddSuccess.jsp";
	}

	@RequestMapping("/deleteAlliance.do")
	public String deletAlliance(@RequestParam("id") long id) {
		allianceService.removeCompanyById(id);
		return "redirect:/toAllianceList.do?status=3";
	}
	//add by wql 2012-3-20
	@RequestMapping("/deleteSelectedAlliance.do")
	public String deleteSelectedAlliance(HttpServletRequest request){
		String [] ids=request.getParameterValues("ids");
		allianceService.deleteSelectedAlliance(ids);
		return "redirect:/toAllianceList.do";
	}
	
	@RequestMapping("/passAlliance.do")
	public String passAlliance(@RequestParam("id")long id,@RequestParam("status")int status) {
		allianceService.passCompanyById(id, status);
		
		System.out.println("passAlliance");
		return "redirect:/toAllianceList.do?status=3";
	}
	
	@RequestMapping("/cancelAlliance.do")
	public String cancelAlliance(@RequestParam("id")long id,@RequestParam("status")int status) {
		System.out.println("cancelAlliance");
		allianceService.cancelCompanyById(id, status);
		return "redirect:/toAllianceList.do?status=3";
	}
	
	@RequestMapping("/rejectAlliance.do")
	public String rejectAlliance(@RequestParam("id")long id,@RequestParam("status")int status) {
		System.out.println("rejectAlliance");
		allianceService.rejectCompanyById(id, status);
		return "redirect:/toAllianceList.do?status=3";
	}

	@RequestMapping("/modifyAlliance.do")
	public String modifyAlliance(AllianceForm allianceForm,HttpServletRequest request,@RequestParam("filename1")MultipartFile filename1) throws IOException {
		Company alliance = new Company();
		try {
			BeanUtils.copyProperties(alliance, allianceForm);
			String filePath =UploadFileUtil.Upload(request, filename1,"alliance");
			alliance.setCertificatesUpload(filePath);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return "redirect:/toAllianceList.do";
	}
	
	//add by lcl 2012-3-29
	@RequestMapping("/toCompanyInfo.do")
	public String toCompanyInfo(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		Company company = (Company)session.getAttribute("user");
		model.addAttribute("company", company);
		return "/view/alliance/companyInfo";
	}
	  /*
     * spring mvc异常处理方法，在controller发生异常，可以以抛出ControllerException，以供捕获
     * */
    @ExceptionHandler({ControllerException.class})  
	   public String exception(ControllerException e,HttpServletRequest request) {  
	       request.setAttribute("error", e.getMessage());
	        return "/admin/error/error";  
	    }  
    
    @RequestMapping(value = "/checkLoginName.do", method = RequestMethod.GET)   
    public @ResponseBody boolean checkLoginName(@RequestParam String username) {   
       /*Personal persona = l;
    	if (personal == null)
    		return true;
    	else
    		return false;*/   
    	return false;
    }   
    
    @RequestMapping(value="/toAllianceQuery.do")
    public String toAllianceQuery(Model model,@RequestParam ("status") String status,HttpServletRequest request){
    	int offset=0;
    	Long maxSize=(long) 0;
    	List<Company> companyList = new ArrayList<Company>();
    
    	
    	try{
    		offset = Integer.valueOf(request.getParameter("pager.offset"));
    	} catch (Exception ex) {
			
    		
    	}
    	if(status.equals("0"))
    	{ companyList = allianceService.getCompany(0, offset, 10);
    	   maxSize = (long) allianceService.getCompany(0).size();
    	   
    	}
    	if(status.equals("1"))
    	{ companyList = allianceService.getCompany(1, offset, 10);
    	 maxSize = (long) allianceService.getCompany(1).size();
    	 
    	}
    	
    	if(status.equals("2"))
    	{  companyList =  allianceService.getCompany(2, offset, 10);
    	   maxSize = (long) allianceService.getCompany(2).size();
    	  
    	}
    	
    	model.addAttribute("status", status);
    	model.addAttribute("maxSize", maxSize);
    	model.addAttribute("companyList", companyList);
    	return "/admin/alliance/allianceQuery";
    }
    
    @RequestMapping("/searchByName.do")
    public String searchByName(HttpServletRequest request,Model model){
    	
    	String keyword = request.getParameter("keyword");
    	keyword = '%'+keyword+'%';
    	List<Company> companyList = allianceService.getCompanyListByName(keyword,0,10);
    	long maxSize = allianceService.getCompanyListByName(keyword).size();
    	
    	model.addAttribute("maxSize", maxSize);
    	model.addAttribute("companyList", companyList);
    	
    	return "/admin/alliance/allianceList";
    }
  //add by lcl 2012-09-10
  	@RequestMapping("/toAllianceBaseInfo.do")
  	public String toAllianceBaseInfo(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
  		Company alliance = (Company)session.getAttribute("alliance");
  		Company allianceInfo = allianceService.getCompanyById(alliance.getId());
  		model.addAttribute("allianceInfo", allianceInfo);
  		
  		return "/view/alliance/allianceBaseInfo";
  	}
  	
 //add by lcl 2012-09-10
  	@RequestMapping("/toAllianceChangePsw.do")
  	public String toAllianceChangePsw(HttpServletRequest request, Model model) {
  		
  		return "/view/alliance/allianceChangePsw";
  	}
  	
  //add by lcl 2012-09-10
  	@RequestMapping("/toAllianceAddSupplyInfo.do")
  	public String toAllianceAddSupplyInfo(HttpServletRequest request, Model model) {
  		
  		return "/view/alliance/allianceAddSupplyInfo";
  	}
  	
  //add by lcl 2012-09-10
  	@RequestMapping("/toAllianceAddDemandInfo.do")
  	public String toAllianceAddDemandInfo(HttpServletRequest request, Model model) {
  		
  		return "/view/alliance/allianceAddDemandInfo";
  	}
  	
  //add by lcl 2012-09-10
  	@RequestMapping("/toAllianceModifySupplyInfoList.do")
  	public String toAllianceModifySupplyInfoList(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
  		Company   allianceCompany = (Company)session.getAttribute("alliance");
  		
  		List<AllianceSDInfo> asiList = allianceSDInfoService.getAllianceSDInfoListByCompanyId(allianceCompany.getId());
  		model.addAttribute("asiList", asiList);
  		
  		return "/view/alliance/allianceSupplyInfoList";
  	}
  	
  //add by lcl 2012-09-10
  	@RequestMapping("/toGetAllianceSupplyInfoById.do")
  	public String toGetAllianceSupplyInfoById(HttpServletRequest request, Model model, @RequestParam ("id") long sdInfoId) {
  		AllianceSupplyParam asp = getAspByAllianceUsername(request, sdInfoId);
  		model.addAttribute("asp", asp);
  		
  		return "/view/alliance/allianceSupplyInfoDetail";
  	}
  	
  	//add by lcl 2012-09-10
  	AllianceSupplyParam getAspByAllianceUsername(HttpServletRequest request, long sdInfoId){
  		HttpSession session = request.getSession();
  		Company    alianceCompany = (Company)session.getAttribute("alliance");
  		//Date createDate = new Date(System.currentTimeMillis());
  		String createDate =DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
  		
  		AllianceSupplyParam asp = new AllianceSupplyParam();
  		asp.setUserName(alianceCompany.getUsername());
  		asp.setCompanyName(alianceCompany.getCompanyname());
  		asp.setCreateDate(createDate);
  		asp.setCompanyAllocation(alianceCompany.getCompanyarea());
  		asp.setCompanyPic(alianceCompany.getCertificatesUpload());
  		
  		asp.setSupplyInfoId(sdInfoId);
  		
  		AllianceSDInfo asdi = allianceSDInfoService.getAllianceSDInfoById(sdInfoId);
  		asp.setProductDescribe(asdi.getProductDescribe());
  		asp.setProductName(asdi.getProductName());
  		asp.setSupplyPrice(asdi.getSupplyPrice());
  		asp.setProductType(asdi.getProductType());
  		asp.setSupplyProductNum(asdi.getSupplyProductNum());
  		asp.setSupplyInfoTitle(asdi.getSupplyInfoTitle());
  		
  		return asp;
  	}
  	
  //add by lcl 2012-09-11
  	@RequestMapping("/toGetAllianceCompanys.do")
  	public String toGetAllianceCompanys(HttpServletRequest request, Model model) {
  		List<Company> allianceCompanyList = allianceService.getAllianceCompanyList();
  		model.addAttribute("allianceCompanyList", allianceCompanyList);
  		
  		return "/view/alliance/allianceCompanyList";
  	}
  	
  //add by lcl 2012-09-10
  	@RequestMapping("/toAllianceModifyDemandInfoList.do")
  	public String toAllianceModifyDemandInfoList(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
  		Company   allianceCompany = (Company)session.getAttribute("alliance");
  		
  		List<AllianceSDInfo> asiList = allianceSDInfoService.getAllianceSDInfoListByCompanyId(allianceCompany.getId());
  		model.addAttribute("asiList", asiList);
  		
  		return "/view/alliance/allianceSupplyInfoList";
  	}
  	
  //add by lcl 2012-09-13
  	@RequestMapping("/toAllianceAddProjectDemandInfo.do")
  	public String toAllianceAddProjectDemandInfo(HttpServletRequest request, Model model) {
  		
  		return "/view/alliance/allianceAddProjectDemandInfo";
  	}
  	
  //add by lcl 2012-09-13
  	@RequestMapping("/toAllianceQueryProductDemandInfo.do")
  	public String toAllianceQueryProductDemandInfo(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
  		Company allianceCompany = (Company)session.getAttribute("alliance");
  		List<AllianceSDInfo> asiList = allianceSDInfoService.getAllianceDemandInfoListByCompanyId(allianceCompany.getId());
  		model.addAttribute("asiList", asiList);
  		
  		return "/view/alliance/allianceProductDemandInfoList";
  	}
  	
  //add by lcl 2012-09-13
  	@RequestMapping("/toAllianceQueryProjectDemandInfo.do")
  	public String toAllianceQueryProjectDemandInfo(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
  		Company allianceCompany = (Company)session.getAttribute("alliance");
  		List<ProjectDemandInfo> pdiList = projectDemandInfoService.getProjectDemandInfoListByCompanyId(allianceCompany.getId());
  		model.addAttribute("pdiList", pdiList);
  		
  		return "/view/alliance/allianceProjectDemandInfoList";
  	}
  	
  //add by lcl 2012-09-13
  	@RequestMapping("/toAllianceAddPageInfo.do")
  	public String toAllianceAddPageInfo(HttpServletRequest request, Model model) {
  		
  		return "/view/alliance/allianceAddCompanyPageInfo";
  	}
  	
  //add by lcl 2012-09-13
  	@RequestMapping("/toAllianceCompanyPage.do")
  	public String toAllianceCompanyPage(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
  		Company sessionCompany = (Company)session.getAttribute("alliance");
  		Company allianceCompany = allianceService.getCompanyById(sessionCompany.getId());
  		model.addAttribute("allianceCompany", allianceCompany);
  		
  		List<AllianceDetail> allianceDetail = allianceDetailService.getAllianceDetailByCompanyId(sessionCompany.getId());
  		if(allianceDetail.size() == 0){
  			model.addAttribute("allianceDetail", null);
  		}else
  			model.addAttribute("allianceDetail", allianceDetail.get(0));
  		
  		List<AllianceSDInfo> allianceSDInfoList = allianceSDInfoService.getAllInfoListByCompanyId(sessionCompany.getId());
  		model.addAttribute("allianceSDInfoList", allianceSDInfoList);
  		
  		return "/view/alliance/allianceCompanyDetailTemplate";
  	}
  	
  	
    //add by lcl 2012-09-30
  	@RequestMapping("/toAllianceAddViewer.do")
  	 public String toAllianceAddViewer(HttpServletRequest request, Model model) {
  	  		
  	  		return "/view/alliance/allianceAddViewer";
  	  	}
  	
  //add by lcl 2012-09-30
  	@RequestMapping("/addAllianceViewer.do")
  	 public String addAllianceViewer(HttpServletRequest request, AllianceViewForm allianceViewForm, Model model) {
  		AllianceViewerInfo viewer= new AllianceViewerInfo();
		try {
			BeanUtils.copyProperties(viewer, viewer);
			String createTime =DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
			HttpSession session = request.getSession();
	  		Company sessionCompany = (Company)session.getAttribute("alliance");
	  		allianceViewForm.setViewPsw(passwordEncoder.encodePassword("shengyin", allianceViewForm.getViewPsw()));
			viewer.setCreateDate(createTime);
			viewer.setCompanyId(sessionCompany.getId());
			viewer.setStatus(allianceViewForm.getStatus());
			viewer.setViewName(allianceViewForm.getViewName());
			viewer.setViewPsw(allianceViewForm.getViewPsw());	
			allianceViewInfoService.createAllianceViewInfo(viewer);
			
			return "/view/alliance/allianceAddViewer";
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return "/view/alliance/allianceAddViewer";
  	  	}
  	
  	@RequestMapping("/allianceUpdateBaseInfo.do")
 	 public String allianceUpdateBaseInfo(HttpServletRequest request, AllianceForm allianceForm, Model model, @RequestParam("filename1")MultipartFile filename1) {
  		HttpSession session = request.getSession();
  		Company allianceCompany = (Company)session.getAttribute("alliance");
  		String username = allianceCompany.getUsername();
  		String password = allianceCompany.getPwd();
  		String email = allianceCompany.getMail();
  		String picPath = allianceCompany.getCertificatesUpload();
		try {
			BeanUtils.copyProperties(allianceCompany, allianceForm);
			
	  		String filePath =UploadFileUtil.Upload(request, filename1,"alliance");
	  		allianceCompany.setCertificatesUpload(filePath);
	  		allianceCompany.setMail(email);
	  		allianceCompany.setUsername(username);
	  		allianceCompany.setPwd(password);
	  		if("".equals(picPath)){
	  			allianceCompany.setCertificatesUpload(picPath);
	  		}
	  		allianceService.updateAlliance(allianceCompany);
   
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return "/view/alliance/allianceBaseInfo";
  	  	}
  	
  	@RequestMapping("/allianceUpdatePsw.do")
	 public String allianceUpdatePsw(AllianceForm allianceForm, Model model, HttpServletRequest request) {
  		HttpSession session = request.getSession();
  		Company allianceCompany = (Company)session.getAttribute("alliance");
  		allianceCompany.setPwd(passwordEncoder.encodePassword("shengyin", allianceForm.getPwd()));
  		allianceService.updateAlliance(allianceCompany);
  		System.out.print(allianceForm.getPwd());
		return "/view/alliance/allianceChangePsw";
 	  	}
  	
  	@RequestMapping("/allianceAddSInfo.do")
	 public String allianceAddSInfo(HttpServletRequest request, AllianceSupplyParam allianceSupplyParam, Model model, @RequestParam("filename1")MultipartFile filename1) {
  		AllianceSDInfo allianceSDInfo = new AllianceSDInfo();
  		HttpSession session = request.getSession();
 		Company allianceCompany = (Company)session.getAttribute("alliance");
 		int status = 1;
		try {
			BeanUtils.copyProperties(allianceSDInfo, allianceSupplyParam);
	  		String filePath =UploadFileUtil.Upload(request, filename1,"alliance");
	  		String createTime =DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
	  		allianceSDInfo.setProductPic(filePath);
	  		allianceSDInfo.setCompanyId(allianceCompany.getId());
	  		allianceSDInfo.setCreateDate(createTime);
	  		allianceSDInfo.setMsgType(status);
	  		
	  		allianceSDInfoService.createSDInfo(allianceSDInfo);
	  		return "/view/alliance/allianceAddSupplyInfo";
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return "/view/alliance/allianceAddSupplyInfo";
 	  	}
  	
  	@RequestMapping("/allianceAddDInfo.do")
	 public String allianceAddDInfo(HttpServletRequest request, AllianceSupplyParam allianceSupplyParam, Model model) {
 		AllianceSDInfo allianceSDInfo = new AllianceSDInfo();
 		HttpSession session = request.getSession();
		Company allianceCompany = (Company)session.getAttribute("alliance");
		int status = 2;
		try {
			BeanUtils.copyProperties(allianceSDInfo, allianceSupplyParam);
	  		String createTime =DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
	  		allianceSDInfo.setCompanyId(allianceCompany.getId());
	  		allianceSDInfo.setCreateDate(createTime);
	  		allianceSDInfo.setMsgType(status);
	  		
	  		allianceSDInfoService.createSDInfo(allianceSDInfo);
	  		return "/view/alliance/allianceAddDemandInfo";
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return "/view/alliance/allianceAddDemandInfo";
	  	}
  	
  	@RequestMapping("/allianceAddProject.do")
	 public String allianceAddProject(HttpServletRequest request, AllianceProjectForm allianceProjectForm, Model model) {
  		ProjectDemandInfo project = new ProjectDemandInfo();
  		HttpSession session = request.getSession();
		Company allianceCompany = (Company)session.getAttribute("alliance");
		try {
			BeanUtils.copyProperties(project, allianceProjectForm);
			project.setProjectDemandCompanyId(allianceCompany.getId());
			String createTime =DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
	  		project.setCreateTime(createTime);
	  		
			projectDemandInfoService.createProject(project);
	  		return "/view/alliance/allianceAddProjectDemandInfo";
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return "/view/alliance/allianceAddProjectDemandInfo";
	  	}
  	
  	@RequestMapping("/allianceAddDetail.do")
	 public String allianceAddDetail(HttpServletRequest request, AllianceDetailForm allianceDetailForm, Model model, @RequestParam("filename1")MultipartFile filename1) {
  		AllianceDetail allianceDetail = new AllianceDetail();
 		HttpSession session = request.getSession();
		Company allianceCompany = (Company)session.getAttribute("alliance");
		int status = 1;
		try {
			BeanUtils.copyProperties(allianceDetail, allianceDetailForm);
	  		String filePath =UploadFileUtil.Upload(request, filename1,"alliance");
	  		String createTime =DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
	  		allianceDetail.setUploadPic(filePath);
	  		allianceDetail.setCompanyId(allianceCompany.getId());
	  		
	  		allianceDetailService.createDetail(allianceDetail);
	  		return "/view/alliance/allianceAddCompanyPageInfo";
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return "/view/alliance/allianceAddCompanyPageInfo";
	  	}
  	
  	@RequestMapping("/getSupplyInfoListByCompany.do")
	 public String getSupplyInfoListByCompany(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
		Company allianceCompany = (Company)session.getAttribute("alliance");
  		List<AllianceSDInfo> asiList =allianceSDInfoService.getSupplyInfoListByCompany(allianceCompany.getId());		  		
  		model.addAttribute("asiList", asiList);
	
		return "/view/alliance/allianceSupplyInfoList";
	  	}
  	
  	@RequestMapping("/getSupplyInfoListByTime.do")
	 public String getSupplyInfoListByTime(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
		Company allianceCompany = (Company)session.getAttribute("alliance");
 		List<AllianceSDInfo> asiList =allianceSDInfoService.getSupplyInfoListByTime(allianceCompany.getId());		  		
 		model.addAttribute("asiList", asiList);
	
		return "/view/alliance/allianceSupplyInfoList";
	  	}
  	
  	@RequestMapping("/getAllSupplyInfoListByTime.do")
	 public String getAllSupplyInfoListByTime(HttpServletRequest request, Model model, @RequestParam("id")int id) {
		List<AllianceSDInfo> asiList =allianceSDInfoService.getAllSupplyInfoListByTime();
		
		//to all supply info page;
		if(id == 0)
			model.addAttribute("type", "all");
		
		//to company supply info page
		if(id == 1)
			model.addAttribute("type", "notall");
		
		model.addAttribute("asiList", asiList);
	
		return "/view/alliance/allianceSupplyInfoListView";
	  	}
  	
  	@RequestMapping("/getSupplyInfoListByCompanyType.do")
	 public String getSupplyInfoListByCompanyType(HttpServletRequest request, Model model, @RequestParam("id")int id) {
		List<AllianceSDInfo> asiList =allianceSDInfoService.getSupplyInfoListByCompanyType();
		//to all supply info page;
		if(id == 0)
			model.addAttribute("type", "all");
				
		//to company supply info page
		if(id == 1)
			model.addAttribute("type", "notall");
		model.addAttribute("asiList", asiList);
	
		return "/view/alliance/allianceSupplyInfoListView";
	  	}
  	
  	 //add by lcl 2012-09-11
  	@RequestMapping("/getCompanyListByCity.do")
  	public String getCompanyListByCity(HttpServletRequest request, Model model) {
  		List<Company> allianceCompanyList = allianceService.getCompanyListByCity();
  		model.addAttribute("allianceCompanyList", allianceCompanyList);
  		
  		return "/view/alliance/allianceCompanyList";
  	}
  	
  //add by lcl 2012-09-11
  	@RequestMapping("/getCompanyListByNature.do")
  	public String getCompanyListByNature(HttpServletRequest request, Model model) {
  		List<Company> allianceCompanyList = allianceService.getCompanyListByNature();
  		model.addAttribute("allianceCompanyList", allianceCompanyList);
  		
  		return "/view/alliance/allianceCompanyList";
  	}
  	
  //add by lcl 2012-09-11
  	@RequestMapping("/getCompanyListByEmployee.do")
  	public String getCompanyListByEmployee(HttpServletRequest request, Model model) {
  		List<Company> allianceCompanyList = allianceService.getCompanyListByEmployee();
  		model.addAttribute("allianceCompanyList", allianceCompanyList);
  		
  		return "/view/alliance/allianceCompanyList";
  	}
  	
  	@RequestMapping("/toAllianceViewList.do")
 	 public String toAllianceViewList(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
		Company allianceCompany = (Company)session.getAttribute("alliance");
 	  	List<AllianceViewerInfo> viewerList = allianceViewInfoService.getAllianceViewerListByAllianceCompanyId(allianceCompany.getId());
 	  	model.addAttribute("viewerList", viewerList);
 	  	
 	  	return "/view/alliance/allianceViewerList";
 	  	}
  	
  	@RequestMapping("/toAllianceModifySupplyInfoListViewer.do")
  	public String toAllianceModifySupplyInfoListViewer(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
  		AllianceViewerInfo   viewer = (AllianceViewerInfo)session.getAttribute("viewer");
  		
  		List<AllianceSDInfo> asiList = allianceSDInfoService.getAllianceSDInfoListByCompanyId(viewer.getCompanyId());
  		model.addAttribute("type", "notall");
  		model.addAttribute("asiList", asiList);
  		
  		return "/view/alliance/allianceSupplyInfoListView";
  	}
  	
  	@RequestMapping("/toAllianceAllSupplyInfoList.do")
  	public String toAllianceAllSupplyInfoList(HttpServletRequest request, Model model) {
  		
  		List<AllianceSDInfo> asiList = allianceSDInfoService.getAllSupplyInfoList();
  		model.addAttribute("asiList", asiList);
  		model.addAttribute("type", "all");
  		
  		return "/view/alliance/allianceSupplyInfoListView";
  	}
  	
  	@RequestMapping("/getDemandInfoListByTime.do")
	 public String getDemandInfoListByTime(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
		Company allianceCompany = (Company)session.getAttribute("alliance");
		List<AllianceSDInfo> asiList =allianceSDInfoService.getDemandInfoListByTime(allianceCompany.getId());
		model.addAttribute("asiList", asiList);
	
		return "/view/alliance/allianceProductDemandInfoList";
	  	}
  	
  	@RequestMapping("/getProjectDemandInfoListByBeginTime.do")
  	public String getProjectDemandInfoListByBeginTime(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
  		Company allianceCompany = (Company)session.getAttribute("alliance");
  		List<ProjectDemandInfo> pdiList = projectDemandInfoService.getProjectDemandInfoListByBeginTime(allianceCompany.getId());
  		model.addAttribute("pdiList", pdiList);
  		
  		return "/view/alliance/allianceProjectDemandInfoList";
  	}
  	
  	@RequestMapping("/getProjectDemandInfoListByCreateTime.do")
  	public String getProjectDemandInfoListByCreateTime(HttpServletRequest request, Model model) {
  		HttpSession session = request.getSession();
  		Company allianceCompany = (Company)session.getAttribute("alliance");
  		List<ProjectDemandInfo> pdiList = projectDemandInfoService.getProjectDemandInfoListByCreateTime(allianceCompany.getId());
  		model.addAttribute("pdiList", pdiList);
  		
  		return "/view/alliance/allianceProjectDemandInfoList";
  	}
  	
  	@RequestMapping("/toAllianceCompanyPageById.do")
  	public String toAllianceCompanyPageById(HttpServletRequest request, Model model,  @RequestParam("id")int id) {
  		Company allianceCompany = allianceService.getCompanyById(id);
  		model.addAttribute("allianceCompany", allianceCompany);
  		
  		List<AllianceDetail> allianceDetail = allianceDetailService.getAllianceDetailByCompanyId(id);
  		
  		if(allianceDetail.size() == 0){
  			model.addAttribute("allianceDetail", null);
  		}else
  			model.addAttribute("allianceDetail", allianceDetail.get(0));
  		
  		List<AllianceSDInfo> allianceSDInfoList = allianceSDInfoService.getAllInfoListByCompanyId(id);
  		model.addAttribute("allianceSDInfoList", allianceSDInfoList);
  		
  		return "/view/alliance/allianceCompanyDetailTemplate";
  	}
  	
  	@RequestMapping("/toAllianceAllQueryProductDemandInfo.do")
  	public String toAllianceAllQueryProductDemandInfo(HttpServletRequest request, Model model) {
  		List<AllianceSDInfo> asiList = allianceSDInfoService.getAllDemandInfoList();
  		model.addAttribute("asiList", asiList);
  		
  		return "/view/alliance/allianceProductDemandInfoListView";
  	}
  	
  	@RequestMapping("/getAllDemandInfoListByTime.do")
	 public String getAllDemandInfoListByTime(HttpServletRequest request, Model model) {
		List<AllianceSDInfo> asiList =allianceSDInfoService.getAllDemandInfoListByTime();
		model.addAttribute("asiList", asiList);
	
		return "/view/alliance/allianceProductDemandInfoListView";
	  	}
  	
  	@RequestMapping("/toAllianceAllQueryProjectDemandInfo.do")
  	public String toAllianceAllQueryProjectDemandInfo(HttpServletRequest request, Model model) {
  		List<ProjectDemandInfo> pdiList = projectDemandInfoService.getAllProjectDemandInfoList();
  		model.addAttribute("pdiList", pdiList);
  		
  		return "/view/alliance/allianceProjectDemandInfoListView";
  	}
  	
  	@RequestMapping("/geAlltProjectDemandInfoListByBeginTime.do")
  	public String geAlltProjectDemandInfoListByBeginTime(HttpServletRequest request, Model model) {
  		List<ProjectDemandInfo> pdiList = projectDemandInfoService.getAllProjectDemandInfoListByBeginTime();
  		model.addAttribute("pdiList", pdiList);
  		
  		return "/view/alliance/allianceProjectDemandInfoListView";
  	}
  	
  	@RequestMapping("/getAllProjectDemandInfoListByCreateTime.do")
  	public String getAllProjectDemandInfoListByCreateTime(HttpServletRequest request, Model model) {
  		List<ProjectDemandInfo> pdiList = projectDemandInfoService.getAllProjectDemandInfoListByCreateTime();
  		model.addAttribute("pdiList", pdiList);
  		
  		return "/view/alliance/allianceProjectDemandInfoListView";
  	}
}
